package web.Project;

import service.User_Service;
import service.impl.User_ServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/addProjectServlet")
public class AddProjectServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置页面编码
        response.setContentType("text/html;charset=utf-8");
        request.setCharacterEncoding("utf-8");
        HttpSession session = request.getSession();

        String project_name = request.getParameter("project_name");
        String project_location = request.getParameter("project_location");
        System.out.println("项目名："+project_name+"项目地址："+project_location);

/*        //获取验证码
        String verifycode = request.getParameter("verifycode");
        String code =(String) session.getAttribute("code");
        if (!code.equalsIgnoreCase(verifycode))
        {
           //验证码错误
            request.setAttribute("addError","验证码错误");
            request.getRequestDispatcher("/project/addProject.jsp").forward(request,response);
            return;
        }*/


        User_Service user_service = new User_ServiceImpl();
        int req = user_service.addProject(project_name,project_location);
        switch (req){
            case 1:
                request.setAttribute("success","添加成功");
                break;
            case 0:
                request.setAttribute("addError","添加失败");
                break;
            case -1:
                request.setAttribute("addError","该项目已存在");
                break;

        }
        request.getRequestDispatcher("/project/addProject.jsp").forward(request,response);

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }
}
